package com.gzsxy.esjy.service.cart.controller.api;

import com.gzsxy.esjy.service.base.dto.request.ApiCartRequest;
import com.gzsxy.esjy.service.base.dto.response.CartRsp;
import com.gzsxy.esjy.service.base.dto.response.DefaultResponse;
import com.gzsxy.esjy.service.cart.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 前台购物车对外接口
 * @date 2022/2/23 17:40
 */
@CrossOrigin  //解决跨域
@RefreshScope    //动态刷新配置文件-外部配置文件
@RestController
@RequestMapping("/api/cart")
@Api("购物车管理")
public class ApiCartController {


    @Autowired
    private CartService cartService;

    @GetMapping("/{userId}")
    @ApiOperation("根据用户id获取当前购物车")
    public DefaultResponse getApiCart(@PathVariable String userId){
        List<CartRsp> cartList = cartService.getApiCart(userId);
        return DefaultResponse.ok(cartList);
    }


    @PutMapping("/edit")
    @ApiOperation("根据购物车id更新商品数量")
    public DefaultResponse editNumber(@RequestBody ApiCartRequest apiCartRequest){
        List<CartRsp> cartList = cartService.editNumber(apiCartRequest);
        return DefaultResponse.ok(cartList);
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除购物车")
    public DefaultResponse deleteCart(@PathVariable String id){
        cartService.deleteCart(id);
        return DefaultResponse.ok();
    }



    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public DefaultResponse addCart(@RequestBody ApiCartRequest apiCartRequest){
        cartService.addCart(apiCartRequest);
        return DefaultResponse.ok();
    }
}
